package com.ruoyi.backend.controller;

import com.google.gson.JsonObject;
import com.ruoyi.backend.dto.SatelliteDTO;
import com.ruoyi.backend.pojo.Satellite;
import com.ruoyi.backend.pojo.TaskParams;
import com.ruoyi.backend.service.EcologicalService;
import com.ruoyi.backend.service.SatelliteService;
import com.ruoyi.backend.util.MinioUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Api(value = "卫星数据管理相关接口")
@RequestMapping("/backend/satellite")
@RestController
@CrossOrigin(origins = "*")
public class SatelliteController {

    @Autowired
    private SatelliteService satelliteService;

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "卫星数据RGB切片元数据获取接口")
    @GetMapping("/metaJson")
    public R<Object> getMeta(String minioObjectName) {

        Object metaJsonContent =  satelliteService.getMeta(minioObjectName);
        return R.ok(metaJsonContent);
    };

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "卫星数据上传服务接口")
    @PostMapping("/upload")
    public R<String> upload(@ModelAttribute @Validated(SatelliteDTO.Upload.class) SatelliteDTO satelliteDTO) throws IOException {

        satelliteService.upload(satelliteDTO);
        return R.ok("卫星数据上传成功");
    }

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "删除卫星数据服务接口")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam("id") @NotNull(message = "卫星ID不能为空") String id) {

        satelliteService.delete(id);
        return R.ok("卫星数据删除成功");
    }

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "更新卫星数据服务接口")
    @PutMapping("/update")
    public R<String> update(@RequestBody @Validated(Satellite.Update.class) Satellite satellite) {

        satelliteService.update(satellite);
        return R.ok("卫星数据更新成功");
    }

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "查询卫星数据服务接口")
    @GetMapping("/search")
    public R<List<Satellite>> search(@RequestParam(name = "mineId", required = false) String mineId,
                                     @RequestParam(name = "productType", required = false) String productType,
                                     @RequestParam(name = "dataSource", required = false) String dataSource,
                                     @RequestParam(name = "productName", required = false) String productName,
                                     @RequestParam(name = "satelliteType", required = false) String satelliteType,
                                     @RequestParam(name = "productLevel", required = false) String productLevel,
                                     @RequestParam(name = "stratTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                     @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime
    ) {

        List<Satellite> satelliteList = satelliteService.search(mineId, productType, dataSource, productName, satelliteType, startTime, endTime, productLevel);
        return R.ok(satelliteList, "卫星数据查询成功");
    }
}
